package com.eunion.web.common.base.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

import com.eunion.web.common.base.BaseAbstractService;
import com.eunion.web.common.base.BaseRepository;
import com.eunion.web.common.base.BaseService;

/**
 * 基本service基类的实现.
 * 
 * @param <T>
 * @param <ID>
 * 
 * @version 2016年1月9日
 */
public abstract class BaseServiceImpl<T, ID extends Serializable> extends BaseAbstractService<T, ID> implements BaseService<T, ID> {

    /**
     * 请定义继承BaseRepository的Repository并在Service层重写BaseServiceImpl.geBaseRepository()。
     */
    public abstract BaseRepository<T, ID> geBaseRepository();

    // @Override
    // public BaseRepository<T, ID> geBaseRepository(){
    // throw new
    // BaseReponsitoryUnExtendException("代码错误。请定义继承BaseRepository的Repository并在Service层重写BaseServiceImpl.geBaseRepository()。");
    // }

    @Override
    public <S extends T> S save(S entity) {
        return (S) geBaseRepository().save(entity);
    }

    @Override
    public <S extends T> List<S> save(Iterable<S> entities) {
        return geBaseRepository().save(entities);
    }

    @Override
    public <S extends T> S saveAndFlush(S entity) {
        return (S) geBaseRepository().saveAndFlush(entity);
    }

    @Override
    public List<T> findAll() {
        return geBaseRepository().findAll();
    }

    @Override
    public List<T> findAll(Sort sort) {
        return geBaseRepository().findAll(sort);
    }

    @Override
    public List<T> findAll(Iterable<ID> ids) {
        return geBaseRepository().findAll(ids);
    }

    @Override
    public void flush() {
        geBaseRepository().flush();
    }

    @Override
    public T getOne(ID id) {
        return (T) geBaseRepository().getOne(id);
    }

    @Override
    public Page<T> findAll(Pageable pageable) {
        return geBaseRepository().findAll(pageable);
    }

    @Override
    public T findOne(ID id) {
        return (T) geBaseRepository().findOne(id);
    }

    @Override
    public boolean exists(ID id) {
        return geBaseRepository().exists(id);
    }

    @Override
    public long count() {
        return geBaseRepository().count();
    }

    @Override
    public void delete(ID id) {
        geBaseRepository().delete(id);
    }

    @Override
    public void delete(T entity) {
        geBaseRepository().delete(entity);
    }

    @Override
    public void delete(Iterable<? extends T> entities) {
        geBaseRepository().delete(entities);
    }

    @Override
    public void deleteAll() {
        geBaseRepository().deleteAll();
    }

    @Override
    public void deleteInBatch(Iterable<T> entities) {
        geBaseRepository().deleteInBatch(entities);
    }

    @Override
    public void deleteAllInBatch() {
        geBaseRepository().deleteAllInBatch();
    }

    @Override
    public Page<T> findAll(Specification<T> spec, Pageable page) {
        return geBaseRepository().findAll(spec, page);
    }

    @Override
    public List<T> findAll(Specification<T> spec, Sort sort) {
        return geBaseRepository().findAll(spec, sort);
    }

    @Override
    public List<T> findAll(Specification<T> spec) {
        return geBaseRepository().findAll(spec);
    }
}
